package com.duomai.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.bumptech.glide.Glide;
import com.duomai.myapplication.R;
import com.duomai.myapplication.base.BaseActivity;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.LunTan;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.LunTanDao;
import com.example.admin.Utils.GlideEngine;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.hawk.Hawk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ForumAddActivity extends BaseActivity {

    LunTanDao lunTanDao;
    ImageView ivImage;
    EditText ettitle, etCont;
    Spinner spinner;

    @Override
    public void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forum_add);
        lunTanDao = new LunTanDao(this);
        ettitle = findViewById(R.id.ettitle);
        etCont = findViewById(R.id.etCont);
        spinner = findViewById(R.id.spinner);
        ivImage = findViewById(R.id.ivImage);
        findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User user = Hawk.get("user");
                String tit = ettitle.getText().toString().trim();
                String cont = etCont.getText().toString().trim();
                String s3 = (String) spinner.getSelectedItem();
                Date currentTime = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = format.format(currentTime);
                LunTan bean = new LunTan();
                bean.luntanTitle = tit;
                bean.luntanContent = cont;
                bean.luntanPic = imgPath;
                bean.luntanTime = time;
                bean.luntanType = s3;
                bean.uid = user.id;
                Log.e(TAG, "onClick: "+bean.toString() );
                lunTanDao.addData(bean);
                finish();
            }
        });
        ivImage.setOnClickListener(v1 -> {
            // 进入相册 以下是例子：用不到的api可以不写
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                    // .theme()//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .maxSelectNum(1)// 最大图片选择数量 int
                    .minSelectNum(1)// 最小选择数量 int
                    .imageSpanCount(4)// 每行显示个数 int
                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                    .previewImage(true)// 是否可预览图片 true or false
                    .isCamera(true)// 是否显示拍照按钮 true or false
                    .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                    .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                    .enableCrop(true)// 是否裁剪 true or false
                    .compress(true)// 是否压缩 true or false
                    .hideBottomControls(true)// 是否显示uCrop工具栏，默认不显示 true or false
                    .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                    .openClickSound(true)// 是否开启点击声音 true or false
                    .previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                    .minimumCompressSize(100)// 小于100kb的图片不压缩
                    .synOrAsy(true)//同步true或异步false 压缩 默认同步
                    .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                    .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                    .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code

        });
    }

    @Override
    public void setInitView() {

    }

    String imgPath;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调

                images = PictureSelector.obtainMultipleResult(data);

                if (images.get(0).isCompressed()) {
                    imgPath = images.get(0).getCompressPath();
                } else {
                    imgPath = images.get(0).getPath();
                }
                Glide.with(this).load(imgPath).into(ivImage);
                //selectList = PictureSelector.obtainMultipleResult(data);

                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的

            }
        }
    }
}